package com.design.ProxyMode.dynamicMode;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * 动态代理类
 */
public class DynamicUserUserProxy {

    public Object createProxy(Object object){
        final Class<?>[] interfaces = object.getClass().getInterfaces();
        DynamicProxyHandler handler = new DynamicProxyHandler(object);
        return Proxy.newProxyInstance(object.getClass().getClassLoader(), interfaces, handler);
    }

    private class DynamicProxyHandler implements InvocationHandler{

        private Object object;

        public DynamicProxyHandler(Object object) {
            this.object = object;
        }

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            long startTime = System.currentTimeMillis();
            final Object invoke = method.invoke(object, args);
            long endTime = System.currentTimeMillis();
            System.out.println("login time : " + (endTime - startTime) +" ms" );
            return invoke;
        }
    }

}
